package cn.zhj.controller;

import cn.zhj.domain.Teacher;
import cn.zhj.query.TeacherQuery;
import cn.zhj.service.ITeacherService;
import cn.zhj.utils.ExcelUtil;
import cn.zhj.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;
    @GetMapping("/{id}")
    //查1
    public R findOne(@PathVariable("id") Integer id){
//        empService.findOne(id);
        return R.ok(teacherService.findOne(id));
    }
    //查全
    @GetMapping
    public R findAll(){
        return R.ok(teacherService.findAll());
    }
    //删除
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Integer id) {

        try {
            teacherService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }

    }
    //增和改
    @PutMapping
    public R update(Teacher teacher) {
        try{
            if (teacher.getId() == null) {
                teacherService.insert(teacher);
            }else {
                teacherService.update(teacher);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }
    //分页+高级
    @PostMapping
    public R queryPage(@RequestBody TeacherQuery teacherQuery) {
        Map<String, Object> map = teacherService.queryPage(teacherQuery);
        return R.ok(map);
    }
    //批量删除
    @DeleteMapping("/patchDeL")
    public R patchDeL(@RequestBody Integer[] ids) {
        teacherService.patchDeL(ids);
        return R.ok();
    }
    //导入
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Teacher> teachers = teacherService.findAll();
        ExcelUtil.exportExcel(teachers,"老师信息","老师",Teacher.class,"teacher",response);
    }
    //导出模版，前端写在导入模态框里是因为导出没有模态框，无法选择导出模版选项
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Teacher> list = new ArrayList<>();
        ExcelUtil.exportExcel(list,"老师信息","老师",Teacher.class,"teacher",response);
    }
    //导出
    @PostMapping("/importExcel")
    public R importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        // 1.使用工具类从复杂文件中获取到老师信息集合
        List<Teacher> teachers = ExcelUtil.importExcel(file, Teacher.class);
        // 2.调用批量新增方法，保存所有老师信息
        //teacherService.patchAdd(teachers);
        System.out.println("导入的数据如下...");
        teachers.forEach(System.out::println);
        //导出不能返回R，因为导出用了response响应，R也是响应，两个响应冲突了，前端接收哪一个响应？
        //导入中没有使用过response，只返回R，不会冲突
        return R.ok();
    }
}
